import { mergeOptions } from "../utils/index";

export function initGlobApi(Vue) {
  Vue.options = {};
  Vue.Mixin = function (mixin) {
    //合并对象
    Vue.options = mergeOptions(this.options, mixin);
  };
  Vue.options.components = {};
  Vue.component = function (id, componentDef) {
    //定义组件方法
    componentDef.name = componentDef.name || id;
    //主要代码
    if (typeof componentDef === "object") {
      componentDef = this.extend(componentDef);
    }
    this.options.components[id] = componentDef; //包含子类实列的对象
  };
  Vue.extend = function (options) {
    let spuer = this;
    //子组件的实列
    const Sub = function vuecomponent(opts) {
      //注意 new Sub().$mount()
      //初始化
      this._init(opts);
    };
    //子组件继承父组件的属性 类的继承
    Sub.prototype = Object.create(spuer.prototype);
    //子组件this的执行 不修改就指向vue了
    Sub.prototype.constructor = Sub;
    //父组件属性合并到子组件
    Sub.options = mergeOptions(this.options, options);
    return Sub;
  };
}
